## Controller Python Script "take_survey"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind state=state
##bind subpath=traverse_subpath
##parameters=page
##title=Take Survey

request = context.REQUEST
_ = context.translate
form = request.form
questions = context.listQuestions(page, skiplabels=1)

member = context.portal_membership.getAuthenticatedMember()
id = context.getUniqueId(member.getId(), request)

for q in questions:
	answer = q.field.widget.process_form(context,q.field,form)[0]
	q.addAnswer(id, answer)

val = request.get('continue', '')
next_page = context.markTaken(member.getId(), id, page)

if val == 'continue':
	status='continue'
	pages = context.getPages()
	next_page = pages[ pages.index(page) + 1]
	return state.set(status='continue', 
					 page=next_page, 
					 portal_status_message=_(msgid='Answers recorded.  Please continue.', domain="archsurvey"))
else:	
	return state.set(status='success', 
                     portal_status_message=_(msgid='Thanks for taking the survey', domain="archsurvey"))
